/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * userPreferencesPanel.java
 *
 * Created on 18/05/2011, 20:14:04
 */

package presentation;

import javax.swing.JOptionPane;

/**
 *
 * @author Andreu Marimon
 */
public class UserPreferences extends javax.swing.JPanel {
    PresentationController _pCntrl;
    /** Creates new form userPreferencesPanel */
    private String _username;
    private String _password;

    private void getUserInfo(){
	_username = _pCntrl.getUsername();
	_password = _pCntrl.getUserPassword();
    }
    public UserPreferences(PresentationController pCntrl) {
        _pCntrl = pCntrl;
	initComponents();
	getUserInfo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userField = new javax.swing.JTextField();
        okPassButton = new defaultJButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        pass1Field = new javax.swing.JPasswordField();
        pass2Field = new javax.swing.JPasswordField();
        okUserButton = new defaultJButton();
        deleteUserButton = new defaultJButton();
        resetModelButton = new defaultJButton();

        setMaximumSize(new java.awt.Dimension(252, 403));
        setMinimumSize(new java.awt.Dimension(252, 403));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(252, 403));

        okPassButton.setText("Ok");
        okPassButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okPassButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("repeat new password");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("change password");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("change username");

        okUserButton.setText("Ok");
        okUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okUserButtonActionPerformed(evt);
            }
        });

        deleteUserButton.setText("Delete User");
        deleteUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteUserButtonActionPerformed(evt);
            }
        });

        resetModelButton.setText("Reset Model");
        resetModelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetModelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(okPassButton)
                    .addComponent(pass2Field, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(okUserButton)
                    .addComponent(userField, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(pass1Field, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(deleteUserButton)
                        .addComponent(resetModelButton)))
                .addGap(59, 59, 59))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel3)
                .addGap(4, 4, 4)
                .addComponent(userField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okUserButton)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pass1Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pass2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okPassButton)
                .addGap(37, 37, 37)
                .addComponent(resetModelButton)
                .addGap(18, 18, 18)
                .addComponent(deleteUserButton)
                .addContainerGap(83, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void okPassButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okPassButtonActionPerformed
		if (pass1Field.getText().equals(pass2Field.getText())) {
			if (!pass1Field.getText().equals("")) {
				_pCntrl.updateUser(_username, pass1Field.getText());
		}
		}
    }//GEN-LAST:event_okPassButtonActionPerformed

	private void okUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okUserButtonActionPerformed
			if (!userField.getText().equals("")) {
				_pCntrl.updateUser(userField.getText(), _password);

			}
	}//GEN-LAST:event_okUserButtonActionPerformed

	private void deleteUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteUserButtonActionPerformed
		int a = JOptionPane.showConfirmDialog(this,"Are you sure you want to delete your user?","Delete User",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE);
			if(a == JOptionPane.YES_OPTION) {
				_pCntrl.deleteUser();
			}
	}//GEN-LAST:event_deleteUserButtonActionPerformed

	private void resetModelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetModelButtonActionPerformed
		int a = JOptionPane.showConfirmDialog(this,"Are you sure you want to reset your model?","Reset Model",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE);
			if(a == JOptionPane.YES_OPTION) {
				_pCntrl.resetModel();
				JOptionPane.showMessageDialog(this,"you need to logout in order to see changes","Info",JOptionPane.INFORMATION_MESSAGE);

			}
	}//GEN-LAST:event_resetModelButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteUserButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton okPassButton;
    private javax.swing.JButton okUserButton;
    private javax.swing.JPasswordField pass1Field;
    private javax.swing.JPasswordField pass2Field;
    private javax.swing.JButton resetModelButton;
    private javax.swing.JTextField userField;
    // End of variables declaration//GEN-END:variables

}
